import {Component, OnInit} from '@angular/core';
import {ActivatedRoute} from '@angular/router';
import {UserService} from '../user.service';
import {Enums, UserMinerState, UserStates, UserTypes} from '../../../public/setting/enums';
import {UserSubAccountComponent} from '../user-sub-account/user-sub-account.component';
import {NzModalService} from 'ng-zorro-antd';

@Component({
  selector: 'app-user-detail',
  templateUrl: './user-detail.component.html',
  styleUrls: ['./user-detail.component.css']
})
export class UserDetailComponent implements OnInit {
  private code: string;
  public userInfo: any = {};//user信息
  public state = Enums.userState;//用户状态
  public userMinerState = Enums.userMinerState;
  public userMinerStates = UserMinerState;
  public userStates = UserStates;       // 用户的状态
  public userType = Enums.userType;       // 是否是领导人
  public userTypes = UserTypes;       // 用户类型
  public ifRefreshList: boolean = false;

  constructor(private userService: UserService, private route: ActivatedRoute, private modalService: NzModalService) {
  }

  ngOnInit() {
    this.code = this.route.snapshot.params.code;//获取参数
    this.loadUserInfo();//查询user类型列表
  }

  /**
   * 查询user信息
   */
  loadUserInfo() {
    this.userService.loadUserByCode(this.code).then((data: any) => {
      if (data) this.userInfo = data;
    });
  }

  userSubAccount(name: string, code: string) {
    this.modalService.create({
      nzTitle: `<span class="color-theme">${name}</span> 的子账户`,
      nzContent: UserSubAccountComponent,
      nzComponentParams: {userCode: code},
      nzWidth: '70%',
      nzMaskClosable: false,
      nzFooter: null
    });
  }

  /**
   * 将用户改为领导人 todo 设为领导人/区域管理者
   * @param event:boolean
   */
  updateToLeader(userType) {
    this.userService.updateToLeader(this.code, userType).then((res) => {
      this.loadUserInfo();
      this.ifRefreshList = true;//修完用户类型后如果返回需要刷新列表页
    });
  }

}
